﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using MvcShop.MVC.Test;
using Castle.MicroKernel.Registration;
using MvcShop.Core.Architecture.Configuration;
using MvcShop.Core.Architecture.Cache;
using System.Web.Mvc;
using System.Reflection;
using MvcShop.Models.Repositories;
using MvcShop.Models;
using MvcShop.Models.Services;
using MvcShop.MVC.Services;

namespace MvcShop.MVC.IOC
{
    public static class AddContainer
    {
        public static IWindsorContainer ConfigureContainer(IWindsorContainer container)
        {

            // automatically register controllers
            container.Register(AllTypes
                                   .Of<Controller>()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));

            // 给注册Windsor容器
            container.Register(
                Component.For<IUserRepository<User>>().ImplementedBy<UserRepository>().LifeStyle.Transient,
                Component.For<IUserInfoRepository<UserInfo>>().ImplementedBy<UserInfoRepository>().LifeStyle.Transient,
                Component.For<IUserRoleRepository<UserRole>>().ImplementedBy<UserRoleRepository>().LifeStyle.Transient,
                Component.For<IUsersService>().ImplementedBy<UsersService>().LifeStyle.Transient,
                Component.For<IRoleRepository<Role>>().ImplementedBy<RoleRepository>().LifeStyle.Transient,
                Component.For<IControllerActionRepository<ControllerAction>>().ImplementedBy<ControllerActionRepository>().LifeStyle.Transient,
                Component.For<IControllerActionRoleRepository<ControllerActionRole>>().ImplementedBy<ControllerActionRoleRepository>().LifeStyle.Transient,
                Component.For<IProductRepository<Product>>().ImplementedBy<ProductRepository>().LifeStyle.Transient,
                Component.For<ICategoryRepository<Category>>().ImplementedBy<CategoryRepository>().LifeStyle.Transient,
                Component.For<IGiftCardRepository<GiftCard>>().ImplementedBy<GiftCardRepository>().LifeStyle.Transient,
                Component.For<IUserAmountRepository<UserAmount>>().ImplementedBy<UserAmountRepository>().LifeStyle.Transient,
                Component.For<IProduct_propertys_contentRepository<Product_propertys_content>>().ImplementedBy<Product_propertys_contentRepository>().LifeStyle.Transient,
                Component.For<IProduct_propertyRepository<Product_property>>().ImplementedBy<Product_propertyRepository>().LifeStyle.Transient,
                Component.For<IProduct_ImageRepository<Product_Image>>().ImplementedBy<Product_ImageRepository>().LifeStyle.Transient,
                Component.For<IProductsService>().ImplementedBy<ProductsService>().LifeStyle.Transient,
                Component.For<IRemarkRepository<Remark>>().ImplementedBy<RemarkRepository>().LifeStyle.Transient,
                Component.For<IOrderRepository<Orders>>().ImplementedBy<OrderRepository>().LifeStyle.Transient,
                Component.For<IOrdersInfoRepository<OrdersInfo>>().ImplementedBy<OrdersInfoRepository>().LifeStyle.Transient,
                Component.For<IDeliverRepository<Deliver>>().ImplementedBy<DeliverRepository>().LifeStyle.Transient,
                Component.For<IAddressBookRepository<AddressBook>>().ImplementedBy<AddressBookRepository>().LifeStyle.Transient,
                Component.For<IFavoriteRepository<Favorite>>().ImplementedBy<FavoriteRepository>().LifeStyle.Transient,
                Component.For<IOnlinePollRepository<OnlinePoll>>().ImplementedBy<OnlinePollRepository>().LifeStyle.Transient,
                Component.For<IPollOptionRepository<PollOption>>().ImplementedBy<PollOptionRepository>().LifeStyle.Transient,
                Component.For<INewsRepository<News>>().ImplementedBy<NewsRepository>().LifeStyle.Transient,
                Component.For<IHelperRepository<Helper>>().ImplementedBy<HelperRepository>().LifeStyle.Transient,
                Component.For<IHelpCategoryRepository<HelpCategory>>().ImplementedBy<HelpCategoryRepository>().LifeStyle.Transient,
                Component.For<IHelperService>().ImplementedBy<HelperService>().LifeStyle.Transient,
                Component.For<IFavoritesService>().ImplementedBy<FavoritesService>().LifeStyle.Transient,
                Component.For<IOrdersService>().ImplementedBy<OrdersService>().LifeStyle.Transient,
                Component.For<IAmountService>().ImplementedBy<AmountService>().LifeStyle.Transient,
                Component.For<IAdsRepository<Ads>>().ImplementedBy<AdsRepository>().LifeStyle.Transient,
                Component.For<IAdPositionRepository<AdPosition>>().ImplementedBy<AdPositionRepository>().LifeStyle.Transient,
                Component.For<IMembershipService>().ImplementedBy<AccountMembershipService>().LifeStyle.Transient,
                Component.For<IFormsAuthenticationService>().ImplementedBy <FormsAuthenticationService >().LifeStyle.Transient,
                Component.For<IKeyWordService>().ImplementedBy<KeyWordService>().LifeStyle.Transient,
                Component.For<ILog>().ImplementedBy<TextFileLog>().LifeStyle.Transient.Named("txtLog").LifeStyle.Transient,
                Component.For<ILogFormatter>().ImplementedBy<PlanFormatter>().LifeStyle.Transient,
                Component.For<ILogFormatter>().ImplementedBy<TextFormatter>().LifeStyle.Transient,
                Component.For<IConfigurationManager>().ImplementedBy<ConfigurationManagerWrapper>().Named("core").LifeStyle.Transient,
                Component.For<ISmtpClient>().ImplementedBy<SmtpClientProxy>().LifeStyle.Transient,
                Component.For<IMsCache>().ImplementedBy<MsCache>().Named("msCache").Parameters(Parameter.ForKey("cacheManagerName").Eq("DefaultCacheManager")).LifeStyle.Transient
                );

            return container;
        }
    }
}
